<?php
declare (strict_types = 1);

namespace app\admin\controller\wx;
use think\Request;
use app\common\model\Material;
class Broadcast extends WxBase
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new \app\common\model\Broadcast;
        $this->validate = new \app\common\validate\Broadcast;
        $this->order = 'id desc';
    }
    /**
     * 添加新数据
     *
     * @return void
     */
    public function save(Request $request){
        $data = $this->request->post();
        $data['author_id'] = $this->admin_id;
        if(!$this->validate->scene('create')->check($data)){
            return json($this->validate->getError(),500);
        }else {
            if($data['send_right_now'] == 1) {
                // {"errcode":0,"errmsg":"send job submission success","msg_id":1000000001}
                $res = $this->send($data);
                if ($res['errcode'] === 0) {
                    $data['status'] = 'send';
                    $data['msg_id'] = $res['msg_id'];
                }else {
                    $data['status'] = 'error';
                }
                $data['result'] = $res;
            }else{
                $data['status'] = 'save';
            }
            $this->model->save($data);
            return json('新增成功！');
        }
    }
    public function preview() {
        $media_id = $this->request->post('media_id',null);
        $wxname = $this->request->post('wxname', null);
        $type = $this->request->post('type', null);
        if(empty($media_id) || empty($wxname) || empty($type)){
            return json('错误的参数', 500);
        }
        $res = '';
        switch ($type) {
            case 'artical':
                # code...
                $res = $this->app->broadcasting->previewNewsByName($media_id,$wxname);
                break;
            case 'text':
                $res = $this->app->broadcasting->previewTextByName($media_id,$wxname);
                break;
            case 'image':
                $res = $this->app->broadcasting->previewImageByName($media_id,$wxname);
                break;
            case 'video':
                $res = $this->app->broadcasting->previewVoiceByName($media_id,$wxname);
                break;
            case 'audio':
                $res = $this->app->broadcasting->previewVideoByName($media_id,$wxname);
                break;
            case 'card':
                $res = $this->app->broadcasting->previewCardByName($media_id, $wxname);
            default:
                # code...
                break;
        }
        if($res['errcode'] === 0) {
            return json('发送预览消息成功！');
        } else{
            return json($res['errmsg'],500);
        }
    }
    private function send($data) {
        $res = '';
        switch ($data['type']) {
            case 'artical':
                # code...
                $res = $this->app->broadcasting->sendNews($data['media_id'],$data['touser']);
                break;
            case 'text':
                $res = $this->app->broadcasting->sendText($data['content'],$data['touser']);
                break;
            case 'image':
                $res = $this->app->broadcasting->sendImage($data['media_id'],$data['touser']);
                break;
            case 'video':
                $material = Material::where('media_id', $data['media_id'])->find();
                $path = '.'.$this->public_path.'/'.$material['filepath'];
                $videoMedia = $this->app->media->uploadVideoForBroadcasting($path, $material['title'], $material['digest']);
                $res = $this->app->broadcasting->sendVideo($videoMedia['media_id'],$data['touser']);
                break;
            case 'audio':
                $res = $this->app->broadcasting->sendVoice($data['media_id'],$data['touser']);
                break;
            default:
                # code...
                break;
        }
        return $res;
    }
    public function status() {
        $msg_id = $this->request->param('msg_id', null);
        $res = $this->app->broadcasting->status($msg_id);
        return json($res);
    }
    public function delete($id) {
        if ($id === null) return json('参数错误！',500);
        $broadcast = $this->model->where('id', $id)->find();
        if($broadcast === null) {
            return json('未找到数据',500);
        }
        if(!empty($broadcast['msg_id'])){
            $res = $this->app->broadcasting->delete($broadcast['msg_id']);
        }
        $this->model->destroy($id);
        return json('删除成功！');

    }
}
